package cn.hl.hm.logic.serviceImpl;

import cn.hl.hm.constants.WithrawalStatus;
import cn.hl.hm.dao.DriverMapper;
import cn.hl.hm.dao.RechargeRecoardWithdrawalMapper;
import cn.hl.hm.entity.RechargeRecoardWithdrawal;
import cn.hl.hm.logic.service.IRechargeRecoardWithdrawalService;
import cn.hl.hm.util.paging.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by xianghan on 17/12/26.
 */
@Service
public class RechargeRecoardWithdrawalServiceImpl implements IRechargeRecoardWithdrawalService {
    private static final Logger logger = LoggerFactory.getLogger(RechargeRecoardWithdrawalServiceImpl.class);
    @Autowired
    private RechargeRecoardWithdrawalMapper withdrawalMapper;
    @Autowired
    private DriverMapper driverMapper;

    @Override
    public RechargeRecoardWithdrawal add(Integer driverId, String driverAccount, String driverName,
                                         String bankName, String bankCard, String cardholder, Double withdrawalCash, String withdrawalReason, String reportPerson, Integer reportUserId) {
        RechargeRecoardWithdrawal rechargeRecoardWithdrawal = new RechargeRecoardWithdrawal();
        rechargeRecoardWithdrawal.setDriverId(driverId);
        rechargeRecoardWithdrawal.setDriverAccount(driverAccount);
        rechargeRecoardWithdrawal.setDriverName(driverName);
        rechargeRecoardWithdrawal.setCardholder(cardholder);
        rechargeRecoardWithdrawal.setWithdrawalReason(withdrawalReason);
        double serviceCharge = 2;
        if (withdrawalCash >= 100 * 100) {
            serviceCharge = 100;
        } else if (withdrawalCash > 2 * 100) {
            serviceCharge = withdrawalCash / 100;
        }
        rechargeRecoardWithdrawal.setWithdrawalCash(new BigDecimal(withdrawalCash));
        rechargeRecoardWithdrawal.setServiceCharge(new BigDecimal(serviceCharge));
        rechargeRecoardWithdrawal.setBankCard(Long.valueOf(bankCard));
        rechargeRecoardWithdrawal.setBankName(bankName);
        rechargeRecoardWithdrawal.setReportPerson(reportPerson);
        rechargeRecoardWithdrawal.setReportUserId(reportUserId);
        rechargeRecoardWithdrawal.setStatus(Byte.valueOf(WithrawalStatus.tjsq.getStatus()));
        rechargeRecoardWithdrawal.setCreateTime(new Date());
        int rows = withdrawalMapper.insertSelective(rechargeRecoardWithdrawal);
        if (rows > 0) {
            return rechargeRecoardWithdrawal;
        }
        return null;
    }

    @Override
    public Boolean operationCheck(Integer id, String status, String auditReason, String assessorUsername, Integer assessorUserId) {
        RechargeRecoardWithdrawal rechargeRecoardWithdrawal = withdrawalMapper.selectByPrimaryKey(id);
        if (rechargeRecoardWithdrawal == null) {
            return false;
        }
        if ("0".equals(status)) {
            rechargeRecoardWithdrawal.setStatus(Byte.valueOf(WithrawalStatus.yyshbtg.getStatus()));
        } else if ("1".equals(status)) {
            rechargeRecoardWithdrawal.setStatus(Byte.valueOf(WithrawalStatus.yyshtg.getStatus()));
        }else{
            return false;
        }
        rechargeRecoardWithdrawal.setOperationAuditReason(auditReason);
        rechargeRecoardWithdrawal.setOperationAssessorUsername(assessorUsername);
        rechargeRecoardWithdrawal.setOperationUserId(assessorUserId);
        Date date = new Date();
        rechargeRecoardWithdrawal.setOperationAssessorTime(date);
        rechargeRecoardWithdrawal.setUpdateTime(date);
        int rows = withdrawalMapper.updateByPrimaryKeySelective(rechargeRecoardWithdrawal);
        if (rows > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean financialCheck(Integer id, String status, String auditReason, String assessorUsername, Integer assessorUserId) {
        RechargeRecoardWithdrawal rechargeRecoardWithdrawal = withdrawalMapper.selectByPrimaryKey(id);
        if (rechargeRecoardWithdrawal == null) {
            return false;
        }
        if ("0".equals(status)) {
            rechargeRecoardWithdrawal.setStatus(Byte.valueOf(WithrawalStatus.cwshbtg.getStatus()));
        } else if ("1".equals(status)) {
            rechargeRecoardWithdrawal.setStatus(Byte.valueOf(WithrawalStatus.cwshwzf.getStatus()));
        }else {
            return false;
        }
        rechargeRecoardWithdrawal.setFinancialAuditReason(auditReason);
        rechargeRecoardWithdrawal.setFinancialAssessorUsername(assessorUsername);
        rechargeRecoardWithdrawal.setFinancialUserId(assessorUserId);
        Date date = new Date();
        rechargeRecoardWithdrawal.setFinancialAssessorTime(date);
        rechargeRecoardWithdrawal.setUpdateTime(date);
        int rows = withdrawalMapper.updateByPrimaryKeySelective(rechargeRecoardWithdrawal);
        if (rows > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean confirmPayment(Integer id, Integer userId) {
        RechargeRecoardWithdrawal rechargeRecoardWithdrawal = withdrawalMapper.selectByPrimaryKey(id);
        if (rechargeRecoardWithdrawal == null) {
            return false;
        }
        rechargeRecoardWithdrawal.setStatus(Byte.valueOf(WithrawalStatus.cwshyzf.getStatus()));
        rechargeRecoardWithdrawal.setFinancialUserId(userId);
        int rows = withdrawalMapper.updateByPrimaryKeySelective(rechargeRecoardWithdrawal);
        if (rows > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<String> operationAssessors() {
        return withdrawalMapper.selectOperationAssessors();
    }

    @Override
    public List<String> financialAssessors() {
        return withdrawalMapper.selectFinancialAssessors();
    }

    @Override
    public List<RechargeRecoardWithdrawal> list(PageInfo pageInfo) {
        List<RechargeRecoardWithdrawal> rechargeRecoardWithdrawalList = withdrawalMapper.selectListPage(pageInfo);
        return rechargeRecoardWithdrawalList;
    }

    @Override
    public RechargeRecoardWithdrawal selectDetail(Integer rrwId) {
        return withdrawalMapper.selectByPrimaryKey(rrwId);
    }

}
